package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkIssueApply;
import com.ysd.lis.entity.WkIssueAssign;
import com.ysd.lis.mapper.WkIssueApplyMapper;
import com.ysd.lis.mapper.WkIssueAssignMapper;
import com.ysd.lis.request.AssignDto;
import com.ysd.lis.request.IssueApplyParam;
import com.ysd.lis.service.WkIssueAssignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-04-15
 */
@Service
@Slf4j
public class WkIssueAssignServiceImpl extends ServiceImpl<WkIssueAssignMapper, WkIssueAssign> implements WkIssueAssignService {
    @Autowired
    WkIssueAssignMapper wkIssueAssignMapper;
    @Autowired
    WkIssueApplyMapper wkIssueApplyMapper;
    @Override
    public Result findIssueAssignList(IssueApplyParam param) {
        MPJLambdaWrapper<WkIssueAssign> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(WkIssueAssign.class)
                .eq(WkIssueAssign::getDelFlag,0)
                .eq(WkIssueAssign::getApplyId,param.getApplyId())
                .orderByAsc(WkIssueAssign::getCreateTime);
        List<WkIssueAssign> detailList = wkIssueAssignMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result submitAssign(AssignDto dto) {
        WkIssueApply apply = new WkIssueApply();
        apply.setId(dto.getId());
        apply.setStatus(dto.getStatus());
        wkIssueApplyMapper.updateById(apply);
        List<WkIssueAssign> assignList = dto.getAssignList();
        //现根据applyId 删除全部
        MPJLambdaWrapper<WkIssueAssign> delWrapper = new MPJLambdaWrapper<>();
        delWrapper.eq(WkIssueAssign::getDelFlag, 0);
        delWrapper.in(WkIssueAssign::getApplyId, assignList.stream().map(WkIssueAssign::getApplyId).collect(Collectors.toList()));
        wkIssueAssignMapper.delete(delWrapper);
        boolean b = this.saveOrUpdateBatch(assignList);
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }

    }
}
